var course = {
			init : function() {
				
				course.courseGrid = $('#courseGrid');
				course.courseMenu = $('#courseMenu');
				course.courseAddDialog = $('#courseAddDialog');
				course.courseAddForm = $('#courseAddForm');
				course.searchCourse = $('#searchCourse');
				course.courseUpdateForm = $('#courseUpdateForm');
				course.courseMenuUpdate = $('#courseMenuUpdate')
				course.courseUpdateDialog = $('#courseUpdateDialog');
				course.name = $('#courseName');
				course.courseName = $('#coursename');
				
				course.courseGrid.treegrid({
				    url: window.ctx +'/CourseServlet?command=listCourse',
				    idField:'id',
				    treeField:'text',
				    fixed: true,
				    animate:true,
				    fitColumns: true,
				    rownumbers: true,
				    singleSelect: true,
				    toolbar: '#courseToolbar',
				    columns:[[
						{field:'id', checkbox: true}, 
						{field:'text', title:'菜单名称', width: 100},
						{field:'user_name', title:'操作者', width: 100},
						{field:'operateTime', title:'操作时间', width: 100},
						{field:'state', title:'状态', width: 100},
						// {field:'memo', title:'备注', width: 100}
				    ]],
				    onLoadSuccess: function(node, data) {
						$(this).treegrid('expandAll')
					},
					onDblClickCell:function(rowIndex, rowData){
						course.toolbar.showUpdateDialog(rowData.id);
					}
				});
	},
	toolbar:{
		showAddDialog: function(id) {
			var getFatherMenu = function(v) {
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx +'/CourseServlet?command=getFatherMenu', {id : v}, function(data) {
					if (data) {
						course.courseAddForm.form('load', {
							id: data.id,
							ptext: data.text
						});
						course.courseAddDialog.dialog('open');
						course.name.focus();
					} else {
						$.messager.alert('系统提示', '获取信息失败！', 'error')
					}
					$.messager.progress('close');
				}, 'json');
			}
			if(id){
				getFatherMenu(id);
			} else {
				var row = course.courseGrid.datagrid('getSelections');
				if(row.length == 1){
					getFatherMenu(row[0].id);
				}else{
					getFatherMenu(0);
				}
				
			}
			
		},
		
		hiddenAddDialog: function() {
			course.courseAddDialog.dialog('close');
			course.toolbar.resetAddForm();
		},
		resetAddForm: function() {
			course.courseAddForm.form('reset');
		},
		add:function () {
			if(course.courseAddForm.form('validate')){
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx +'/CourseServlet?command=addCourse', course.courseAddForm.serialize(), function(data) {
					if (data == 1) {
						$.messager.show({
							title: '系统提示',
							msg: '新增成功！',
							timeout: 3000,
							showType: 'slide'
						});
						course.toolbar.resetAddForm();
						course.courseGrid.treegrid('reload');	
					} else {
						$.messager.alert('系统提示', '新增失败！', 'error')
					}
					$.messager.progress('close');
					course.toolbar.hiddenAddDialog();
				});
			}
		},
		showUpdateDialog:function(id){
			var getcourse = function(v) {
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx +'/CourseServlet?command=getCourse', {id : v}, function(data) {
					if (data) {
						course.courseUpdateForm.form('load', {
							id: data.id,
							ptext: data.ptext,
							courseName:data.text,
							
						});
						course.courseUpdateDialog.dialog('open');
						course.courseName.focus();
					} else {
						$.messager.alert('系统提示', '获取信息失败！', 'error')
					}
					$.messager.progress('close');
				}, 'json');
			}
			if(id){
				getcourse(id);
			}else{
				var row = course.courseGrid.datagrid('getSelections');
				if(row.length==1){
					getcourse(row[0].id);
				}else{
					$.messager.show({
						title: '系统提示',
						msg: '请选择一行数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
				
			}
		},
		hiddenUpdateDialog: function() {
			course.courseUpdateDialog.dialog('close');
			course.toolbar.resetUpdateForm();
		},
		resetUpdateForm: function() {
			course.courseUpdateForm.form('reset');
		},
		
		update:function () {
			if(course.courseUpdateForm.form('validate')){
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx +'/CourseServlet?command=updateCourse', course.courseUpdateForm.serialize(), function(data) {
					if (data == 1) {
						$.messager.show({
							title: '系统提示',
							msg: '修改成功！',
							timeout: 3000,
							showType: 'slide'
						});
						course.toolbar.resetUpdateForm();
						course.courseGrid.treegrid('reload');		
					} else {
						$.messager.alert('系统提示', '修改失败！', 'error')
					}
					$.messager.progress('close');
					course.toolbar.hiddenUpdateDialog();
				});
			}
	},
	remove: function() {
		var row = course.courseGrid.datagrid('getSelections');
		if (row.length > 0) {
			var id = [];
			$.each(row, function(i, v) {
				id.push(v.id);
			});
			$.post(window.ctx +'/CourseServlet?command=deleteCourse', {id: id.join(',')}, function(data) {
				if (data == 1) {
					$.messager.show({
						title: '系统提示',
						msg: '删除成功！',
						timeout: 3000,
						showType: 'slide'
					});
					course.courseGrid.treegrid('reload');
				} else {
					$.messager.alert('系统提示', '删除失败！', 'error')
				}
				$.messager.progress('close');
			});
		} else {
			$.messager.show({
				title: '系统提示',
				msg: '请选择数据进行删除！',
				timeout: 3000,
				showType: 'slide'
			});	
		}
	},
	updateState:function(id){
		var getupdateState = function(v) {
			$.messager.progress({
				text : '正在处理...'
			});
			$.post(window.ctx + '/CourseServlet?command=getupdateState', {id : v}, function(data) {
				if (data) {
					$.messager.show({
						title: '系统提示',
						msg: '修改成功！',
						timeout: 3000,
						showType: 'slide'
					});
					course.courseGrid.treegrid('reload');
				} else {
					$.messager.alert('系统提示', '获取信息失败！', 'error')
				}
				$.messager.progress('close');
			});
		}
		if(id){
			getupdateState(id);
		}else{
			var row = course.courseGrid.datagrid('getSelections');
			if(row.length==1){
				getupdateState(row[0].id);
			}else{
				$.messager.show({
					title: '系统提示',
					msg: '请选择一行数据！',
					timeout: 3000,
					showType: 'slide'
				});
			}
			
		}
	},
		redo: function() {
			course.courseGrid.datagrid('unselectAll');
		}
	}

}
$(function() {
	course.init();
});